---
title: "Gender Protesting 2023 Appendix Analysis"
author: "Martin Naunov"
date: "06/14/2022"
output: pdf_document
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning=FALSE,message=FALSE)
setwd("/Users/martinnaunov/Desktop/Gender Protesting Data")
```

```{r}
setwd("/Users/martinnaunov/Desktop/Gender Protesting Data")
gp_1 <- read.csv("gp_c.csv")

```


##### ------------------  APPENDIX 1: SUMMARY STATISTICS ----------------- ##### 


#Table A.1.2.1: Sample Statistics

```{r}
#AGE (Binary)

gp_1$age_18_34 <- NA
gp_1$age_35_54 <- NA
gp_1$age_55_over <- NA

gp_1$age_18_34 <- ifelse(gp_1$age_3 == 1, 1, 0)
gp_1$age_35_54 <- ifelse(gp_1$age_3 == 2, 1, 0)
gp_1$age_55_over <- ifelse(gp_1$age_3 == 3, 1, 0)
  
  

#INCOME (Binary)

gp_1$income_1 <- NA
gp_1$income_2 <- NA
gp_1$income_3 <- NA
gp_1$income_4 <- NA
gp_1$income_5 <- NA
gp_1$income_6 <- NA
gp_1$income_7 <- NA
gp_1$income_8 <- NA


gp_1$income_1 <- ifelse(gp_1$income_b <=3, 1, 0)
gp_1$income_2 <- ifelse(gp_1$income_b == 4, 1, 0)
gp_1$income_3 <- ifelse(gp_1$income_b == 5, 1, 0)
gp_1$income_4 <- ifelse(gp_1$income_b == 6, 1, 0)
gp_1$income_5 <- ifelse(gp_1$income_b == 7, 1, 0)
gp_1$income_6 <- ifelse(gp_1$income_b == 8, 1, 0)
gp_1$income_7 <- ifelse(gp_1$income_b == 9, 1, 0)
gp_1$income_8 <- ifelse(gp_1$income_b == 10, 1, 0)


```

```{r Standardize}

range01 <- function(x,na.rm = T){(x-min(x, na.rm = T))/(max(x,na.rm = T)-min(x,na.rm = T))}

gp_1$violence_1_st <- range01(gp_1$violence_1)

gp_1$violence_2_st <- range01(gp_1$violence_2)

gp_1$toler_1_index_st <- range01(gp_1$toler_1_index)

gp_1$toler_2_index_st <- range01(gp_1$toler_2_index)

gp_1$mor_1_st <- range01(gp_1$mor_1)

```


```{r}
library(dplyr)
gp_1_sample_stats <- gp_1 %>% dplyr::select(uni, orthodox, sex_b, putin_support_st, age_18_34, age_35_54, age_55_over, income_1,income_2,income_3,income_4,income_5,income_6,income_7,income_8,putin_support_st, violence_1_st,violence_2_st,toler_1_index_st,toler_2_index_st,mor_1_st)

```


```{r}
library(stargazer)
stargazer(gp_1_sample_stats)
```


##### ------------------  APPENDIX 2: TUKEY'S HSD RESULTS ----------------- ##### 

#Table A.2.1: Tukey’s HSD test results

```{r}
gp_1$condition_f <- as.factor(gp_1$condition)

```

```{r}
v_1_model <- aov(violence_1 ~ condition_f, data = gp_1)
summary(v_1_model)

v1hsd <- TukeyHSD(v_1_model)
```


```{r}

v_2_model <- aov(violence_2 ~ condition_f, data = gp_1)

v2hsd <- TukeyHSD(v_2_model)
```

```{r}

t_1_model <- aov(toler_1_index ~ condition_f, data = gp_1)

t1hsd <- TukeyHSD(t_1_model)
```

```{r}

t_2_model <- aov(toler_2_index ~ condition_f, data = gp_1)

t2hsd <- TukeyHSD(t_2_model)
```

```{r}

m_model <- aov(mor_1 ~ condition_f, data = gp_1)
mhsd <- TukeyHSD(m_model)

```

```{r}
library(broom)
v1hsd_df<- tidy(v1hsd)
v2hsd_df<- tidy(v2hsd)
t1hsd_df<- tidy(t1hsd)
t2hsd_df<- tidy(t2hsd)
mhsd_df<- tidy(mhsd)

```

```{r}
v1hsd_df_2 <- v1hsd_df[c("contrast", "estimate","conf.low","conf.high", "adj.p.value")]
v2hsd_df_2 <- v2hsd_df[c("contrast", "estimate","conf.low","conf.high", "adj.p.value")]
t1hsd_df_2 <- t1hsd_df[c("contrast", "estimate","conf.low","conf.high", "adj.p.value")]
t2hsd_df_2 <- t2hsd_df[c("contrast", "estimate","conf.low","conf.high", "adj.p.value")]
mhsd_df_2 <- mhsd_df[c("contrast", "estimate","conf.low","conf.high", "adj.p.value")]

```

```{r}
v1hsd_df_2$outcome <- "Preemptive Violence Perception"
mhsd_df_2$outcome <- "Immorality Perception"
t1hsd_df_2$outcome <- "Preemptive Repression Toleration"
v2hsd_df_2$outcome <- "Believing Authorities that Protesters were Violent"
t2hsd_df_2$outcome <- "Reactive Repression Toleration"

```

```{r}
hsd_df_t <- rbind(v1hsd_df_2, mhsd_df_2,t1hsd_df_2, v2hsd_df_2, t2hsd_df_2)

hsd_df_total <-hsd_df_t[!(hsd_df_t$contrast=="3-2" | hsd_df_t$contrast=="4-2"),]

hsd_df_total_check <- hsd_df_total
```

```{r}
library(dplyr)


#In my analysis, I want to compare patriarchy-compliant (Condition 3) versus patriarchy-defiant (Condition 4) female protesters. The default Tukey HSD output presents the contrast as Condition 4 minus Condition 3 (4-3). To align the results with my intended comparison, I invert the mean 4-3 difference (i.e. I multiply it by -1). Similarly, the lower and upper bounds of the 95% confidence intervals are also multiplied by -1 and then swapped.  This allows us to interpret the results in the context of the effect of patriarchy-compliant versus patriarchy-defiant female protesters (i.e. Condition 3 minus Condition 4 as opposed to Condition 4 minus Condition 3). 

i1 <- hsd_df_total$contrast=="4-3"
hsd_df_total$estimate[i1] <- hsd_df_total$estimate[i1] * (-1)
hsd_df_total$conf.low[i1] <- hsd_df_total$conf.low[i1] * (-1)
hsd_df_total$conf.high[i1] <- hsd_df_total$conf.high[i1] * (-1)

hsd_df_total[i1, c("conf.low", "conf.high")] <- hsd_df_total[i1, c("conf.high", "conf.low")]



hsd_df_total$contrast<-dplyr::recode(hsd_df_total$contrast, 
                              "2-1"= "Generic Women \nvs. Men", 
                              "3-1"="Patriarchy-Compliant Women \nvs. Men", 
                              "4-1"="Patriarchy-Defiant Women \nvs. Men", 
                              "4-3"="Patriarchy-Compliant Women \nvs. Patriarchy-Defiant Women")

```

```{r}
library(dplyr)
hsd_df_total_2 <- hsd_df_total %>% dplyr::select(outcome, everything())
```


```{r}
library(dplyr)

#Sample Size Calculation
length(gp_1$violence_1[!is.na(gp_1$violence_1)])
length(gp_1$mor_1[!is.na(gp_1$mor_1)])
length(gp_1$toler_1_index[!is.na(gp_1$toler_1_index)])
length(gp_1$violence_2[!is.na(gp_1$violence_2)])
length(gp_1$toler_2_index[!is.na(gp_1$toler_2_index)])

```

```{r}
library(xtable)
xtable(hsd_df_total_2)

```


##### ------------------  APPENDIX 3: DEMOGRAPHIC COVARIATES ----------------- ##### 


#3.1 Balance Tests
```{r Age Balance}
library(dplyr)
age_balancing <- group_by(gp_1, condition_f) %>%
  summarise(
    count = n(),
    mean = mean(age_b_st, na.rm = TRUE),
    sd = sd(age_b_st, na.rm = TRUE)
  )

age_balancing <- subset(age_balancing, !is.na(condition_f))

age_balancing$dv <- NA
age_balancing$dv <- "Age"

```

```{r Sex Balance}
sex_balancing <- group_by(gp_1, condition_f) %>%
  summarise(
    count = n(),
    mean = mean(sex_b, na.rm = TRUE),
    sd = sd(sex_b, na.rm = TRUE)
  )

sex_balancing <- subset(sex_balancing, !is.na(condition_f))

sex_balancing$dv <- NA
sex_balancing$dv <- "Sex"
```

```{r Edu Balance}
edu_balancing <- group_by(gp_1, condition_f) %>%
  summarise(
    count = n(),
    mean = mean(uni, na.rm = TRUE),
    sd = sd(uni, na.rm = TRUE)
  )


edu_balancing <- subset(edu_balancing, !is.na(condition_f))

edu_balancing$dv <- NA
edu_balancing$dv <- "Tertiary Education"

```

```{r Putin Support Balance}
putin_support_balancing <- group_by(gp_1, condition_f) %>%
  summarise(
    count = n(),
    mean = mean(putin_support_st, na.rm = TRUE),
    sd = sd(putin_support_st, na.rm = TRUE)
  )

putin_support_balancing <- subset(putin_support_balancing, !is.na(condition_f))

putin_support_balancing$dv <- NA
putin_support_balancing$dv <- "Putin Support"
```

```{r Income Balance}
income_balancing <- group_by(gp_1, condition_f) %>%
  summarise(
    count = n(),
    mean = mean(income_b_st, na.rm = TRUE),
    sd = sd(income_b_st, na.rm = TRUE)
  )

income_balancing <- subset(income_balancing, !is.na(condition_f))

income_balancing$dv <- NA
income_balancing$dv <- "Income"
```

```{r Religion Balance}
religion_balancing <- group_by(gp_1, condition_f) %>%
  summarise(
    count = n(),
    mean = mean(orthodox, na.rm = TRUE),
    sd = sd(orthodox, na.rm = TRUE)
  )

religion_balancing <- subset(religion_balancing, !is.na(condition_f))

religion_balancing$dv <- NA
religion_balancing$dv <- "Religion (Orthodox)"
```

```{r}
summary_df_balancing <- rbind(age_balancing, sex_balancing, edu_balancing, putin_support_balancing, religion_balancing, income_balancing)

summary_df_balancing <- summary_df_balancing[c("condition_f", "mean","sd","dv")]

```


#Figure A.3.1
```{r}
balance_gp <- summary_df_balancing

balance_gp$mean <- as.numeric(balance_gp$mean)
balance_gp$sd <- as.numeric(balance_gp$sd)
```

```{r}
balance_gp$group <-dplyr::recode(balance_gp$condition_f, "1"="Men", "2"="Generic Women", "3"="Patriarchy-Compliant Women", "4"="Patriarchy-Defiant Women")

```

```{r}
library(ggplot2)


plot_dvs<-ggplot(balance_gp, aes(x = mean, y = dv, group=group, color=group))+ geom_point(aes(shape=group, color=group), size=2, position = position_dodge(width = 0.50))+ geom_errorbar(aes(xmin=mean-sd, xmax=mean+sd, group=group), size=1, width=0.3,position = position_dodge(width = 0.50)) + geom_vline(xintercept = 0,lty=2) + labs(title = "") + theme_bw()+theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5), axis.text.y = element_text(color = "grey20", size = 13, face = "bold"),legend.text=element_text(size=13)) + xlab("") + ylab("")

pl_dvs <- plot_dvs + labs(color = " ", shape=" ")
p_ds <- pl_dvs + theme(legend.position = "bottom")
p_ds

ggsave("balance_plot_check.png", p_ds, width = 10, height = 6, dpi = 300,
       units = "in")



```



#3.2 Demographic Covariates as Controls (Table A.3.1)

```{r}
gp_1$condition_f <- as.factor(gp_1$condition)

```

```{r}
v_1_model_ci <- aov(violence_1 ~ condition_f +age_b_st + sex_b + income_b_st + putin_support_st+ orthodox + uni, data = gp_1)
summary(v_1_model_ci)
nrow(v_1_model_ci$model) # 1334
v_1_model_ci_tukey <- TukeyHSD(v_1_model_ci, "condition_f")

```

```{r}
library(broom)
v_1_model_ci_tukey_df<- tidy(v_1_model_ci_tukey)
v_1_model_ci_tukey_df$outcome <- "Preemptive Violence Perception"
v_1_model_ci$outcome <- "Preemptive Violence Perception"

```


```{r}

v_2_model_ci <- aov(violence_2 ~ condition_f +age_b_st + sex_b + income_b_st + putin_support_st+ orthodox + uni, data = gp_1)
nrow(v_2_model_ci$model) #1334
v_2_model_ci_tukey <- TukeyHSD(v_2_model_ci, "condition_f")
```

```{r}
library(broom)
v_2_model_ci_tukey_df<- tidy(v_2_model_ci_tukey)
v_2_model_ci_tukey_df$outcome <- "Believing Authorities that Protesters were Violent"
v_2_model_ci$outcome <- "Believing Authorities that Protesters were Violent"

```

```{r}

t_1_model_ci <- aov(toler_1_index ~ condition_f +age_b_st + sex_b + income_b_st + putin_support_st+ orthodox + uni, data = gp_1)
nrow(t_1_model_ci$model) #1333
t_1_model_ci_tukey <- TukeyHSD(t_1_model_ci, "condition_f")
```

```{r}
library(broom)
t_1_model_ci_tukey_df<- tidy(t_1_model_ci_tukey)
t_1_model_ci_tukey_df$outcome <- "Preemptive Repression Toleration"
t_1_model_ci$outcome <- "Preemptive Repression Toleration"

```

```{r}

t_2_model_ci <- aov(toler_2_index ~ condition_f +age_b_st + sex_b + income_b_st + putin_support_st+ orthodox + uni, data = gp_1)
nrow(t_2_model_ci$model) #1334
t_2_model_ci_tukey <- TukeyHSD(t_2_model_ci, "condition_f")
```

```{r}
library(broom)
t_2_model_ci_tukey_df<- tidy(t_2_model_ci_tukey)
t_2_model_ci_tukey_df$outcome <- "Reactive Repression Toleration"
t_2_model_ci$outcome <- "Reactive Repression Toleration"

```

```{r}

m_model_ci <- aov(mor_1 ~ condition_f +age_b_st + sex_b + income_b_st + putin_support_st+ orthodox + uni, data = gp_1)
m_model_ci_tukey <- TukeyHSD(m_model_ci, "condition_f")
nrow(m_model_ci$model) #1333


```

```{r}
library(broom)
m_model_ci_tukey_df<- tidy(m_model_ci_tukey)
m_model_ci_tukey_df$outcome <- "Immorality Perception"
m_model_ci$outcome <- "Immorality Perception"

```

```{r}
total_controls <- rbind(v_1_model_ci_tukey_df, m_model_ci_tukey_df,t_1_model_ci_tukey_df, v_2_model_ci_tukey_df, t_2_model_ci_tukey_df)

plot_models_controls<-total_controls[!(total_controls$contrast=="3-2" | total_controls$contrast=="4-2"),]


#In my analysis, I want to compare patriarchy-compliant (Condition 3) versus patriarchy-defiant (Condition 4) female protesters. The default Tukey HSD output presents the contrast as Condition 4 minus Condition 3 (4-3). To align the results with my intended comparison, I invert the mean 4-3 difference (i.e. I multiply it by -1). Similarly, the lower and upper bounds of the 95% confidence intervals are also multiplied by -1 and then swapped.  This allows us to interpret the results in the context of the effect of patriarchy-compliant versus patriarchy-defiant female protesters (i.e. Condition 3 minus Condition 4 as opposed to Condition 4 minus Condition 3). 


i1 <- plot_models_controls$contrast=="4-3"
plot_models_controls$estimate[i1] <- plot_models_controls$estimate[i1] * (-1)
plot_models_controls$conf.low[i1] <- plot_models_controls$conf.low[i1] * (-1)
plot_models_controls$conf.high[i1] <- plot_models_controls$conf.high[i1] * (-1)

plot_models_controls[i1, c("conf.low", "conf.high")] <- plot_models_controls[i1, c("conf.high", "conf.low")]


plot_models_controls$contrast<-dplyr::recode(plot_models_controls$contrast, "2-1"="Generic Women \nvs. Men", "3-1"="Patriarchy-Compliant Women \nvs. Men", "4-1"="Patriarchy-Defiant Women \nvs. Men", "4-3"="Patriarchy-Compliant Women \nvs. Patriarchy-Defiant Women")



```

```{r}
library(dplyr)
plot_models_controls_2 <- plot_models_controls %>% dplyr::select(outcome, everything())
```

```{r}
plot_models_controls_2 <- plot_models_controls_2[c("outcome", "contrast", "estimate","conf.low","conf.high", "adj.p.value")]
```

#TABLE A.3.1: TUKEY HSD TEST RESULTS (CONTROLS INCLUDED)

```{r}
xtable(plot_models_controls_2)

```



#APPENDIX 3.3 TREATMENT HETEROGENEITY

#Analysis for Table A.3.10: Demographic Covariates as Moderators of the Treatment Effect


```{r}
#Sex
g_th_t1 <- aov(toler_1_index ~ condition_f*sex_b, data = gp_1) 
g_th_t2 <- aov(toler_2_index ~ condition_f*sex_b, data = gp_1) 

# Support for Putin
ps_th_t1 <- aov(toler_1_index ~ condition_f*putin_support_st, data = gp_1) 
ps_th_t2 <-  aov(toler_2_index ~ condition_f*putin_support_st, data = gp_1)

# University
uni_th_t1 <- aov(toler_1_index ~ condition_f*uni, data = gp_1) 
uni_th_t2 <- aov(toler_2_index ~ condition_f*uni, data = gp_1) 


#Religion (Orthodox)
rel_th_t1 <- aov(toler_1_index ~ condition_f*orthodox, data = gp_1) 
rel_th_t2 <- aov(toler_2_index ~ condition_f*orthodox, data = gp_1)

#Age
age_th_t1 <- aov(toler_1_index ~ condition_f*age_b_st, data = gp_1) 
age_th_t2 <- aov(toler_2_index ~ condition_f*age_b_st, data = gp_1) 

#Income
income_th_t1 <- aov(toler_1_index ~ condition_f*income_b_st, data = gp_1) 
income_th_t2 <- aov(toler_2_index ~ condition_f*income_b_st, data = gp_1) 

```

```{r}
library(xtable)

#Sex (0 is a woman & 1 is a man)

xtable(g_th_t1)
xtable(g_th_t2)


# Support for Putin
xtable(ps_th_t1)
xtable(ps_th_t2)

#University
xtable(uni_th_t1)
xtable(uni_th_t2)


#Religion
xtable(rel_th_t1)
xtable(rel_th_t2)

#Age
xtable(age_th_t1)
xtable(age_th_t2)

#Income
xtable(income_th_t1)
xtable(income_th_t2)

#I consolidated these multiple tables into a single comprehensive table (Table A.3.10) in the appendix.

```
#Sample size calculation (for Tabl A.3.10)
```{r}
length(gp_1$toler_1_index[!is.na(gp_1$toler_1_index)] & gp_1$sex_b[!is.na(gp_1$sex_b)])
#1350

length(gp_1$toler_2_index[!is.na(gp_1$toler_2_index)] & gp_1$sex_b[!is.na(gp_1$sex_b)])
#1350

length(gp_1$toler_1_index[!is.na(gp_1$toler_1_index)] & gp_1$putin_support_st[!is.na(gp_1$putin_support_st)])
# 1348

length(gp_1$toler_2_index[!is.na(gp_1$toler_2_index)] & gp_1$putin_support_st[!is.na(gp_1$putin_support_st)])
# 1349


length(gp_1$toler_1_index[!is.na(gp_1$toler_1_index)] & gp_1$uni[!is.na(gp_1$uni)])
# 1348

length(gp_1$toler_2_index[!is.na(gp_1$toler_2_index)] & gp_1$uni[!is.na(gp_1$uni)])
# 1349


length(gp_1$toler_1_index[!is.na(gp_1$toler_1_index)] & gp_1$orthodox[!is.na(gp_1$orthodox)])
# 1350

length(gp_1$toler_2_index[!is.na(gp_1$toler_2_index)] & gp_1$orthodox[!is.na(gp_1$orthodox)])
# 1350


length(gp_1$toler_1_index[!is.na(gp_1$toler_1_index)] & gp_1$age_b_st[!is.na(gp_1$age_b_st)])
# 1350

length(gp_1$toler_2_index[!is.na(gp_1$toler_2_index)] & gp_1$age_b_st[!is.na(gp_1$age_b_st)])
# 1350

length(gp_1$toler_1_index[!is.na(gp_1$toler_1_index)] & gp_1$income_b_st[!is.na(gp_1$income_b_st)])
# 1350

length(gp_1$toler_2_index[!is.na(gp_1$toler_2_index)] & gp_1$income_b_st[!is.na(gp_1$income_b_st)])
# 1350

```



#Analysis for Tables A.3.2 to A.3.9: The moderating effect of respondents’ sex

#Create new dataframes 
#one that has only conditions 3 & 4
#one that has only conditions 1 & 3
#one that has only conditions 1 & 4
#one that has only conditions 1 & 2

```{r}
library(tidyr)

gp_tm <- subset(gp_1, condition_f!=2)
gp_tm <- subset(gp_tm, condition_f!=4)
gp_tm$condition_f
gp_tm$cond_3 <- NA
gp_tm$cond_3 <- ifelse(gp_tm$condition_f == 3, 1, 0) # going from men (0) to traditional women (1)

gp_tn <- subset(gp_1, condition_f!=1)
gp_tn <- subset(gp_tn, condition_f!=2)
gp_tn$condition_f
gp_tn$cond_3 <- NA
gp_tn$cond_3 <- ifelse(gp_tn$condition_f == 3, 1, 0) # going from non-traditional (0) to traditional women (1)

gp_tn <- gp_tn %>% drop_na(toler_1_index)

gp_ntm <- subset(gp_1, condition_f!=2)
gp_ntm <- subset(gp_ntm, condition_f!=3)
gp_ntm$condition_f
gp_ntm$cond_3 <- NA
gp_ntm$cond_3 <- ifelse(gp_ntm$condition_f == 4, 1, 0) # going from men (0) to non-traditional women (1)

gp_ntm <- gp_ntm %>% drop_na(toler_1_index)


gp_gm <- subset(gp_1, condition_f!=3)
gp_gm <- subset(gp_gm, condition_f!=4)
gp_gm$condition_f
gp_gm$cond_3 <- NA
gp_gm$cond_3 <- ifelse(gp_gm$condition_f == 2, 1, 0) # going from men (0) to generic women (1)
```


#Table A.3.2: The moderating effect of respondents’ sex (Experimental Conditions: Men vs. Generic Women)

```{r}
library(xtable)
g_t1_gm <-  aov(toler_1_index ~ condition_f*sex_b, data = gp_gm) #no significance
summary(g_t1_gm)

xtable(g_t1_gm)


#Sample size calculation
length(gp_gm$toler_1_index[!is.na(gp_gm$toler_1_index)] & gp_gm$sex_b[!is.na(gp_gm$sex_b)])
#675


g_t2_gm <-  aov(toler_2_index ~ condition_f*sex_b, data = gp_gm) #no significance
summary(g_t2_gm)

xtable(g_t2_gm)


#Sample size calculation
length(gp_gm$toler_2_index[!is.na(gp_gm$toler_2_index)] & gp_gm$sex_b[!is.na(gp_gm$sex_b)])
#675

```


#Table A.3.3: The moderating effect of respondents’ sex (Experimental Conditions: Men vs. Patriarchy- Compliant Women)

```{r}

g_t1_tm <- aov(toler_1_index ~ condition_f*sex_b, data = gp_tm) #no significance
summary(g_t1_tm)

xtable(g_t1_tm)

#Sample size calculation
length(gp_tm$toler_1_index[!is.na(gp_tm$toler_1_index)] & gp_tm$sex_b[!is.na(gp_tm$sex_b)])
#665


g_t2_tm <- aov(toler_2_index ~ condition_f*sex_b, data = gp_tm) #no significance
summary(g_t2_tm)

xtable(g_t2_tm)


#Sample size calculation
length(gp_tm$toler_2_index[!is.na(gp_tm$toler_2_index)] & gp_tm$sex_b[!is.na(gp_tm$sex_b)])
#665

```


#Table A.3.4: The moderating effect of respondents’ sex (Experimental Conditions: Patriarchy- Compliant Women vs. Patriarchy-Defiant Women)


```{r}
g_t1_tn <- aov(toler_1_index ~ condition_f*sex_b, data = gp_tn) #significant
summary(g_t1_tn)

xtable(g_t1_tn)

#Sample size calculation
length(gp_tn$toler_1_index[!is.na(gp_tn$toler_1_index)] & gp_tn$sex_b[!is.na(gp_tn$sex_b)])
#673


g_t2_tn <- aov(toler_2_index ~ condition_f*sex_b, data = gp_tn) #significant
summary(g_t2_tn)

xtable(g_t2_tn)


length(gp_tn$toler_2_index[!is.na(gp_tn$toler_2_index)] & gp_tn$sex_b[!is.na(gp_tn$sex_b)])
#673

```


#Table A.3.5: The moderating effect of respondents’ sex (Experimental Conditions: Men vs. Patriarchy- Defiant Women)
```{r}
g_t1_ntm <- aov(toler_1_index ~ condition_f*sex_b, data = gp_ntm) #significant
summary(g_t1_ntm)

xtable(g_t1_ntm)

#Sample size calculation
length(gp_ntm$toler_1_index[!is.na(gp_ntm$toler_1_index)] & gp_ntm$sex_b[!is.na(gp_ntm$sex_b)])
#670


g_t2_ntm <- aov(toler_2_index ~ condition_f*sex_b, data = gp_ntm) #significant
summary(g_t2_ntm)

xtable(g_t2_ntm)

#Sample size calculation
length(gp_ntm$toler_2_index[!is.na(gp_ntm$toler_2_index)] & gp_ntm$sex_b[!is.na(gp_ntm$sex_b)])
#670


```


#Analysis for Tables A.3.6-9 (Subset Analysis)

```{r}
library(broom)
# 0 is a woman & 1 is a man

#MEN T1 TN

g_t1_tn_m <- aov(toler_1_index ~ condition_f, data = subset(gp_tn, sex_b==1))
g_t1_tn_m_tukey <- TukeyHSD(g_t1_tn_m)
g_t1_tn_m_tukey_df <- tidy(g_t1_tn_m_tukey)
g_t1_tn_m_tukey_df$outcome <- "Preemptive Repression Tolerance"
nrow(g_t1_tn_m$model) #280

#MEN T2 TN

g_t2_tn_m <- aov(toler_2_index ~ condition_f, data = subset(gp_tn, sex_b==1))
g_t2_tn_m_tukey <- TukeyHSD(g_t2_tn_m)
g_t2_tn_m_tukey_df <- tidy(g_t2_tn_m_tukey)
g_t2_tn_m_tukey_df$outcome <- "Reactive Repression Tolerance"
nrow(g_t2_tn_m$model) #280
```


```{r}


#MEN T1 NTM

g_t1_ntm_m <- aov(toler_1_index ~ condition_f, data = subset(gp_ntm, sex_b==1))
g_t1_ntm_m_tukey <- TukeyHSD(g_t1_ntm_m)
g_t1_ntm_m_tukey_df <- tidy(g_t1_ntm_m_tukey)
g_t1_ntm_m_tukey_df$outcome <- "Preemptive Repression Tolerance"
nrow(g_t1_ntm_m$model) #273

#MEN T2 NTM

g_t2_ntm_m <- aov(toler_2_index ~ condition_f, data = subset(gp_ntm, sex_b==1))
g_t2_ntm_m_tukey <- TukeyHSD(g_t2_ntm_m)
g_t2_ntm_m_tukey_df <- tidy(g_t2_ntm_m_tukey)
g_t2_ntm_m_tukey_df$outcome <- "Reactive Repression Tolerance"
nrow(g_t2_ntm_m$model) #273
```


```{r}
total_subset_men_tn <- rbind(g_t1_tn_m_tukey_df,g_t2_tn_m_tukey_df)

i1 <- total_subset_men_tn$contrast=="4-3"

total_subset_men_tn$estimate[i1] <- total_subset_men_tn$estimate[i1] * (-1)
total_subset_men_tn$conf.low[i1] <- total_subset_men_tn$conf.low[i1] * (-1)
total_subset_men_tn$conf.high[i1] <- total_subset_men_tn$conf.high[i1] * (-1)

total_subset_men_tn[i1, c("conf.low", "conf.high")] <- total_subset_men_tn[i1, c("conf.high", "conf.low")]

```

```{r}
library(dplyr)
total_subset_men_tn_2 <- total_subset_men_tn %>% dplyr::select(outcome, everything())
```

```{r}
total_subset_men_tn_2 <- total_subset_men_tn_2[c("outcome", "estimate","conf.low","conf.high", "adj.p.value")]
```

#Table A.3.6: Subset Analysis for Male Respondents (Experimental Conditions: Patriarchy-Compliant vs. Patriarchy-Defiant Women) 

```{r}
xtable(total_subset_men_tn_2)

```


#Table A.3.7: Subset Analysis for Male Respondents (Conditions: Men vs. Patriarchy-Defiant Women)
```{r}
library(dplyr)

total_subset_men_ntm <- rbind(g_t1_ntm_m_tukey_df, g_t2_ntm_m_tukey_df)

total_subset_men_ntm_2 <- total_subset_men_ntm %>% dplyr::select(outcome, everything())

total_subset_men_ntm_2 <- total_subset_men_ntm_2[c("outcome", "estimate","conf.low","conf.high", "adj.p.value")]

xtable(total_subset_men_ntm_2)

```



```{r}
# 0 is a woman & 1 is a man

#WOMEN T1 TN

g_t1_tn_w <- aov(toler_1_index ~ condition_f, data = subset(gp_tn, sex_b==0))
g_t1_tn_w_tukey <- TukeyHSD(g_t1_tn_w)

g_t1_tn_w_tukey_df <- tidy(g_t1_tn_w_tukey)
g_t1_tn_w_tukey_df$outcome <- "Preemptive Repression Tolerance"
nrow(g_t1_tn_w$model) #393

#WOMEN T2 TN

g_t2_tn_w <- aov(toler_2_index ~ condition_f, data = subset(gp_tn, sex_b==0))
g_t2_tn_w_tukey <- TukeyHSD(g_t2_tn_w)
g_t2_tn_w_tukey_df <- tidy(g_t2_tn_w_tukey)
g_t2_tn_w_tukey_df$outcome <- "Reactive Repression Tolerance"
nrow(g_t2_tn_w$model) #393

```


```{r}

#WOMEN NTM T1
g_t1_ntm_w <- aov(toler_1_index ~ condition_f, data = subset(gp_ntm, sex_b==0))
g_t1_ntm_w_tukey <- TukeyHSD(g_t1_ntm_w)
g_t1_ntm_w_tukey_df <- tidy(g_t1_ntm_w_tukey)
g_t1_ntm_w_tukey_df$outcome <- "Preemptive Repression Tolerance"
nrow(g_t1_ntm_w$model) #397


#WOMEN NTM T2

g_t2_ntm_w <- aov(toler_2_index ~ condition_f, data = subset(gp_ntm, sex_b==0))
g_t2_ntm_w_tukey <- TukeyHSD(g_t2_ntm_w)
g_t2_ntm_w_tukey_df <- tidy(g_t2_ntm_w_tukey)
g_t2_ntm_w_tukey_df$outcome <- "Reactive Repression Tolerance"
nrow(g_t2_ntm_w$model) #397

```

#Table A.3.8: Subset Analysis for Female Respondents (Conditions: Patriarchy-Compliant vs. -Defiant Women)

```{r}
library(dplyr)

total_subset_women_tn <- rbind(g_t1_tn_w_tukey_df,g_t2_tn_w_tukey_df)

i1 <- total_subset_women_tn$contrast=="4-3"

total_subset_women_tn$estimate[i1] <- total_subset_women_tn$estimate[i1] * (-1)
total_subset_women_tn$conf.low[i1] <- total_subset_women_tn$conf.low[i1] * (-1)
total_subset_women_tn$conf.high[i1] <- total_subset_women_tn$conf.high[i1] * (-1)

total_subset_women_tn[i1, c("conf.low", "conf.high")] <- total_subset_women_tn[i1, c("conf.high", "conf.low")]


total_subset_women_tn_2 <- total_subset_women_tn %>% dplyr::select(outcome, everything())


total_subset_women_tn_2 <- total_subset_women_tn_2[c("outcome", "estimate","conf.low","conf.high", "adj.p.value")]

xtable(total_subset_women_tn_2)

```

#Table A.3.9: Subset Analysis for Female Respondents (Conditions: Men vs. Patriarchy-Defiant Women)


```{r}
library(dplyr)

total_subset_women_ntm <- rbind(g_t1_ntm_w_tukey_df, g_t2_ntm_w_tukey_df)

total_subset_women_ntm_2 <- total_subset_women_ntm %>% dplyr::select(outcome, everything())

total_subset_women_ntm_2 <- total_subset_women_ntm_2[c("outcome", "estimate","conf.low","conf.high", "adj.p.value")]

xtable(total_subset_women_ntm_2)

```


##### ------------------  APPENDIX 4: MANIPULATION CHECKS ----------------- ##### 


#Protesters Description
```{r}

mytable_p <- table(gp_1$Q48, gp_1$condition_f)
ftable(mytable_p)
```

```{r}
gp_1$man_1 <- NA
gp_1$man_2 <- NA
gp_1$man_3 <- NA
gp_1$man_4 <- NA

gp_1$man_1 <- ifelse(gp_1$condition_f ==1 & gp_1$Q48=="Мужчины", 1, 0)

gp_1$man_2 <- ifelse(gp_1$condition_f ==2 & gp_1$Q48=="Женщины", 1, 0)

gp_1$man_3 <- ifelse(gp_1$condition_f ==3 & gp_1$Q48=="Женщины,Матери" | gp_1$Q48=="Матери", 1, 0)

gp_1$man_4 <- ifelse(gp_1$condition_f ==4 & gp_1$Q48=="Женщины,Феминистки" | gp_1$Q48=="Феминистки", 1, 0)


gp_1$man_gender <- gp_1$man_1 + gp_1$man_2 + gp_1$man_3 + gp_1$man_4

summary(gp_1$man_gender)


```

#Table A.4.1.1: Results of a manipulation check (gender of protesters)
```{r}
library(dplyr)
t411 <- group_by(gp_1, condition_f) %>%
  summarise(
    count = n(),
    mean = mean(man_gender, na.rm = TRUE),
    sd = sd(man_gender, na.rm = TRUE)
  )

summary(gp_1$man_gender)
#Mean:0.8363 

xtable(t411)
# 331 + 344 + 334 + 340 (N=1349)


```


```{r}
library(broom)

man_gender_model <- aov(man_gender ~ condition_f, data = gp_1)

man_gender_model_tukey <- TukeyHSD(man_gender_model)

man_gender_model_tukey_df<- tidy(man_gender_model_tukey)

```


#Table A.4.1.2: Differences between experimental condition on success of manipulating protesters’ gender 
```{r}
library(dplyr)

man_gender_model_tukey_df_2 <- man_gender_model_tukey_df[c("contrast", "estimate","conf.low","conf.high", "adj.p.value")]


man_gender_model_tukey_df_2_t <-man_gender_model_tukey_df_2[!(man_gender_model_tukey_df_2$contrast=="3-2" | man_gender_model_tukey_df_2$contrast=="4-2"),]

#In my analysis, I want to compare patriarchy-compliant (Condition 3) versus patriarchy-defiant (Condition 4) female protesters. The default Tukey HSD output in R presented the contrast as Condition 4 minus Condition 3. To align this with my intended comparison, I invert the mean 4-3 difference (i.e. I multiply it by -1). Similarly, the lower and upper bounds of the 95% confidence intervals are also multiplied by -1 and then swapped.  This allows us to interpret the results in the context of the effect of patriarchy-compliant versus patriarchy-defiant female protesters (i.e. 3-4 as opposed to 4-3). 

i1 <- man_gender_model_tukey_df_2_t$contrast=="4-3"
man_gender_model_tukey_df_2_t$estimate[i1] <- man_gender_model_tukey_df_2_t$estimate[i1] * (-1)
man_gender_model_tukey_df_2_t$conf.low[i1] <- man_gender_model_tukey_df_2_t$conf.low[i1] * (-1)
man_gender_model_tukey_df_2_t$conf.high[i1] <- man_gender_model_tukey_df_2_t$conf.high[i1] * (-1)


man_gender_model_tukey_df_2_t[i1, c("conf.low", "conf.high")] <- man_gender_model_tukey_df_2_t[i1, c("conf.high", "conf.low")]
```


```{r}
xtable(man_gender_model_tukey_df_2_t)

#Sample Size Calculation
complete_rows_count_man_check_gen <- sum(complete.cases(gp_1[c("man_gender", "condition_f")]))
print(complete_rows_count_man_check_gen)

#1349
```


#Figure A.4.1.1: The effect of experimental condition on success of manipulating protesters’ gender 

```{r}
library(ggplot2)
plot_mcpd <- man_gender_model_tukey_df_2_t

plot_mcpd$group_f<-as.factor(plot_mcpd$contrast)

plot_mcpd$group_f<-dplyr::recode(plot_mcpd$group_f, "2-1"="Generic Women \nvs. Men", "3-1"="Patriarchy-Compliant \nWomen vs. Men", "4-1"="Patriarchy-Defiant \nWomen vs. Men", "4-3"="Patriarchy-Compliant Women \nvs. Patriarchy-Defiant Women")


plot_mcpd$group_fd <- factor(plot_mcpd$group_f, levels = c("Patriarchy-Compliant Women \nvs. Patriarchy-Defiant Women","Patriarchy-Defiant \nWomen vs. Men","Patriarchy-Compliant \nWomen vs. Men", "Generic Women \nvs. Men"))

plot_mcpd2 <-ggplot(plot_mcpd, aes(x = estimate, y = group_fd))+ geom_point()+ geom_errorbar(aes(xmin=conf.low, xmax=conf.high), size=1, width=0.2) + geom_vline(xintercept = 0,lty=2) + labs(title = "Differences in Means", subtitle = "Tukey Simultaneous 95% CIs") + theme_bw()+theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5)) + xlab("") + ylab("") 


plot_mcpd2 <- plot_mcpd2 + theme(axis.text=element_text(size=13, face="bold"))
plot_mcpd2


ggsave("plot_mcpd_check_upd.png", plot_mcpd2, width = 10, height = 4.5, dpi = 300,
       units = "in")
```


# Table A.4.2.1: Results of a manipulation check (protest issue)
```{r}
library(dplyr)
library(tidyr)

gp_1$mc_i_2<-dplyr::recode(gp_1$Q47, "Ухудшение экономики"=1, 
                         "Коррупция"=0, "Права ЛГБТ"=0,"Прозрачность выборов"=0,
                         "Иностранное вмешательство"=0,"Другая"=0)

library(dplyr)
group_by(gp_1, condition_f) %>%
  summarise(
    count = n(),
    mean = mean(mc_i_2, na.rm = TRUE),
    sd = sd(mc_i_2, na.rm = TRUE)
  )

summary(gp_1$mc_i_2)

# 331 + 344 + 334 + 340 (N=1349)
```



#A.4.3: Conditioning on Manipulation Checks (Table & Figure A.4.3.1)

```{r}
m_model_manip2_v1 <- aov(violence_1 ~ condition_f +age_b_st + sex_b + income_b_st + putin_support_st+ uni + orthodox + mc_i_2 + man_gender, data = gp_1)
v1hsdmc <- TukeyHSD(m_model_manip2_v1, "condition_f")
summary(m_model_manip2_v1)


length(residuals(m_model_manip2_v1))
#1334 obsevations
```


```{r}
m_model_manip2_v2 <- aov(violence_2 ~ condition_f +age_b_st + sex_b + income_b_st + putin_support_st+ uni + orthodox + mc_i_2 + man_gender, data = gp_1)
v2hsdmc <- TukeyHSD(m_model_manip2_v2, "condition_f")

length(residuals(m_model_manip2_v2))
#1334 obsevations

```

```{r}
m_model_manip2_t1 <- aov(toler_1_index ~ condition_f +age_b_st + sex_b + income_b_st + putin_support_st+ uni + orthodox + mc_i_2 + man_gender, data = gp_1)
t1hsdmc <- TukeyHSD(m_model_manip2_t1, "condition_f")


length(residuals(m_model_manip2_t1))
#1333 obsevations
```

```{r}
m_model_manip2_t2 <- aov(toler_2_index ~ condition_f +age_b_st + sex_b + income_b_st + putin_support_st+ uni + orthodox + mc_i_2 + man_gender, data = gp_1)
t2hsdmc <- TukeyHSD(m_model_manip2_t2, "condition_f")

length(residuals(m_model_manip2_t2))
#1334 obsevations
```

```{r}
m_model_manip2_mor <- aov(mor_1 ~ condition_f +age_b_st + sex_b + income_b_st + putin_support_st+ uni + orthodox + mc_i_2 + man_gender, data = gp_1)
mhsdmc <- TukeyHSD(m_model_manip2_mor, "condition_f")

length(residuals(m_model_manip2_mor))
#1333 obsevations
```


```{r}
library(broom)
v1hsdmc_df<- tidy(v1hsdmc)
mhsdmc_df <- tidy(mhsdmc)
t1hsdmc_df<- tidy(t1hsdmc)
v2hsdmc_df<- tidy(v2hsdmc)
t2hsdmc_df<- tidy(t2hsdmc)
```


```{r}
library(dplyr)

v1hsdmc_df_2 <- v1hsdmc_df[c("contrast", "estimate","conf.low","conf.high", "adj.p.value")]
mhsdmc_df_2 <- mhsdmc_df[c("contrast", "estimate","conf.low","conf.high", "adj.p.value")]
t1hsdmc_df_2 <- t1hsdmc_df[c("contrast", "estimate","conf.low","conf.high", "adj.p.value")]
v2hsdmc_df_2 <- v2hsdmc_df[c("contrast", "estimate","conf.low","conf.high", "adj.p.value")]
t2hsdmc_df_2 <- t2hsdmc_df[c("contrast", "estimate","conf.low","conf.high", "adj.p.value")]


v1hsdmc_df_2$dv <- "1"
mhsdmc_df_2$dv <- "2"
t1hsdmc_df_2$dv <- "3"
v2hsdmc_df_2$dv <- "4"
t2hsdmc_df_2$dv <- "5"


hsd_df_mc <- rbind(v1hsdmc_df_2,mhsdmc_df_2,t1hsdmc_df_2,v2hsdmc_df_2,t2hsdmc_df_2)

hsd_df_mc_t <-hsd_df_mc[!(hsd_df_mc$contrast=="3-2" | hsd_df_mc$contrast=="4-2"),]

i1 <- hsd_df_mc_t$contrast=="4-3"
hsd_df_mc_t$estimate[i1] <- hsd_df_mc_t$estimate[i1] * (-1)
hsd_df_mc_t$conf.low[i1] <- hsd_df_mc_t$conf.low[i1] * (-1)
hsd_df_mc_t$conf.high[i1] <- hsd_df_mc_t$conf.high[i1] * (-1)

hsd_df_mc_t[i1, c("conf.low", "conf.high")] <- hsd_df_mc_t[i1, c("conf.high", "conf.low")]


```

#TABLE A.4.3.1

```{r}

xtable(hsd_df_mc_t)

```


#FIGURE A.4.3.1
```{r}

plot_models_man_checks_incl <- hsd_df_mc_t

library(ggplot2)

plot_models_man_checks_incl$names <- plot_models_man_checks_incl$dv

plot_models_man_checks_incl$names<-dplyr::recode(plot_models_man_checks_incl$names, 
                          "1"="Violence \nPerception",
                          "2"="Immorality \nPerception",
                          "3"="Preemptive Repression \nToleration",
                          "4"="Believing Authorities that \nProtesters were Violent",
                          "5"="Reactive Repression \nToleration")

plot_models_man_checks_incl$namesf<-as.factor(plot_models_man_checks_incl$names)

plot_models_man_checks_incl$namesfd <- factor(plot_models_man_checks_incl$namesf, levels = c("Reactive Repression \nToleration","Believing Authorities that \nProtesters were Violent","Preemptive Repression \nToleration","Immorality \nPerception","Violence \nPerception"))


plot_models_man_checks_incl$group_f<-as.factor(plot_models_man_checks_incl$contrast)

plot_models_man_checks_incl$group_f<-dplyr::recode(plot_models_man_checks_incl$group_f, "2-1"="Generic Women \nvs. Men", "3-1"="Patriarchy-Compliant \nWomen vs. Men", "4-1"="Patriarchy-Defiant \nWomen vs. Men", "4-3"="Patriarchy-Compliant Women \nvs. Patriarchy-Defiant Women")


plot_models_man_checks_incl$group_fd <- factor(plot_models_man_checks_incl$group_f, levels = c("Patriarchy-Compliant Women \nvs. Patriarchy-Defiant Women","Patriarchy-Defiant \nWomen vs. Men","Patriarchy-Compliant \nWomen vs. Men", "Generic Women \nvs. Men"))


manipulation_checks_model_plot <-ggplot(plot_models_man_checks_incl, aes(x = estimate, y = namesfd, group=group_f, color=group_f))+ geom_point(aes(shape=group_f, color=group_f), size=2, position = position_dodge(width = 0.50))+ geom_errorbar(aes(xmin=conf.low, xmax=conf.high, group=group_f), size=1, width=0.3,position = position_dodge(width = 0.50)) + geom_vline(xintercept = 0,lty=2) + labs(title = "Differences in Means", subtitle = "Tukey Simultaneous 95% CIs") + theme_bw()+theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5), axis.text.y = element_text(color = "grey20", size = 13, face = "bold"),legend.text=element_text(size=11)) + xlab("") + ylab("")

manipulation_checks_model_plot <- manipulation_checks_model_plot + labs(color = " ", shape=" ")
manipulation_checks_model_plot <- manipulation_checks_model_plot + theme(legend.position = "bottom")
manipulation_checks_model_plot


ggsave("manipulation_checks_model_plot_upd.png", manipulation_checks_model_plot, width = 10, height = 7, dpi = 300,
       units = "in")

```


##### ------------------  APPENDIX 5: WEIGHTED ANALYSIS ----------------- ##### 


#Below is code for recreating Table A.5.1: Tukey HSD Test Results Weighted by Respondent’s Gender and Age

```{r}
library(survey)
library(tidyr)
gp_nona <- gp_1 %>% drop_na(edu)

small.svy.unweighted <- svydesign(ids=~1, data=gp_nona)
```


```{r}
age.dist <- data.frame(age_3 = c("1", "2","3"),
                       Freq = nrow(gp_nona) * c(0.34, 0.35,0.31))
sex.dist <- data.frame(sex_b = c("0", "1"),
                       Freq = nrow(gp_nona) * c(0.55, 0.45))
```


```{r}

small.svy.rake <- rake(design = small.svy.unweighted,
                   sample.margins = list(~age_3, ~sex_b),
                   population.margins = list(age.dist, sex.dist))
```


```{r}
summary(weights(small.svy.rake))
```

```{r}
small.svy.rake.trim <- trimWeights(small.svy.rake, lower=0.3, upper=3,
                                  strict=TRUE) 
```

```{r}
trimmed_weights <- weights(small.svy.rake.trim)
class(trimmed_weights)
length(trimmed_weights)
```

```{r}

v_1_w <- aov(violence_1 ~ condition_f, data = gp_nona,weights=trimmed_weights)

v1_w_hsd <- TukeyHSD(v_1_w)

```

```{r}

t_1_w <- aov(toler_1_index ~ condition_f, data = gp_nona, weights=trimmed_weights)

t1_w_hsd <- TukeyHSD(t_1_w)

```



```{r}

v_2_w <- aov(violence_2 ~ condition_f, data = gp_nona,weights=trimmed_weights)

v2_w_hsd <- TukeyHSD(v_2_w)

```


```{r}

t_2_w <- aov(toler_2_index ~ condition_f, data = gp_nona,weights=trimmed_weights)

t2_w_hsd <- TukeyHSD(t_2_w)

```


```{r}


m_w <- aov(mor_1 ~ condition_f, data = gp_nona,weights=trimmed_weights)

m_w_hsd <- TukeyHSD(m_w)


```

```{r}
library(broom)
v1whsd_df<- tidy(v1_w_hsd)
v2whsd_df<- tidy(v2_w_hsd)
t1whsd_df<- tidy(t1_w_hsd)
t2whsd_df<- tidy(t2_w_hsd)
mwhsd_df<- tidy(m_w_hsd)

```

```{r}
v1whsd_df_2 <- v1whsd_df[c("contrast", "estimate","conf.low","conf.high", "adj.p.value")]
v2whsd_df_2 <- v2whsd_df[c("contrast", "estimate","conf.low","conf.high", "adj.p.value")]
t1whsd_df_2 <- t1whsd_df[c("contrast", "estimate","conf.low","conf.high", "adj.p.value")]
t2whsd_df_2 <- t2whsd_df[c("contrast", "estimate","conf.low","conf.high", "adj.p.value")]
mwhsd_df_2 <- mwhsd_df[c("contrast", "estimate","conf.low","conf.high", "adj.p.value")]

```

```{r}
v1whsd_df_2$outcome <- "Preemptive Violence Perception"
mwhsd_df_2$outcome <- "Immorality Perception"
t1whsd_df_2$outcome <- "Preemptive Repression Toleration"
v2whsd_df_2$outcome <- "Believing Authorities that Protesters were Violent"
t2whsd_df_2$outcome <- "Reactive Repression Toleration"

```

```{r}
hsd_w_df_t <- rbind(v1whsd_df_2, mwhsd_df_2,t1whsd_df_2, v2whsd_df_2, t2whsd_df_2)

hsd_w_df_total <-hsd_w_df_t[!(hsd_w_df_t$contrast=="3-2" | hsd_w_df_t$contrast=="4-2"),]


```

```{r}
i1 <- hsd_w_df_total$contrast=="4-3"
hsd_w_df_total$estimate[i1] <- hsd_w_df_total$estimate[i1] * (-1)
hsd_w_df_total$conf.low[i1] <- hsd_w_df_total$conf.low[i1] * (-1)
hsd_w_df_total$conf.high[i1] <- hsd_w_df_total$conf.high[i1] * (-1)

hsd_w_df_total[i1, c("conf.low", "conf.high")] <- hsd_w_df_total[i1, c("conf.high", "conf.low")]


hsd_w_df_total$contrast<-dplyr::recode(hsd_w_df_total$contrast,"2-1"="Generic Women \nvs. Men", "3-1"="Patriarchy-Compliant \nWomen vs. Men", "4-1"="Patriarchy-Defiant \nWomen vs. Men", "4-3"="Patriarchy-Compliant Women \nvs. Patriarchy-Defiant Women")
```

```{r}
library(dplyr)
hsd_w_df_total_2 <- hsd_w_df_total %>% dplyr::select(outcome, everything())
```

```{r}
xtable(hsd_w_df_total_2)

```

##### ------------------  APPENDIX 6: T-TEST ANALYSIS ----------------- ##### 

#Table A.6.1: t-test results comparing public reactions to male and female protesters
```{r}
v1_t_gm <- t.test(violence_1 ~ condition_f,data = gp_gm)
m1_t_gm <- t.test(mor_1 ~ condition_f,data = gp_gm)
v2_t_gm <- t.test(violence_2 ~ condition_f,data = gp_gm)
t1_t_gm <- t.test(toler_1_index ~ condition_f,data = gp_gm)
t2_t_gm <- t.test(toler_2_index ~ condition_f,data = gp_gm)

```

```{r}
library(broom)
library(purrr)

tab_gm <- map_df(list(v1_t_gm, m1_t_gm, v2_t_gm, t1_t_gm, t2_t_gm), tidy)
```

```{r}
tab_gm_2 <- tab_gm[c("estimate1", "estimate2","estimate","statistic", "p.value", "parameter")]
```

```{r}
variables <- data.frame(matrix(NA,    # Create empty data frame
                          nrow = 5,
                          ncol = 1))

variables[1, 1] = "Violence Perception"
variables[2, 1] = "Immorality Perception"
variables[3, 1] = "Preemptive Repression Toleration"
variables[4, 1] = "Believing Authorities that Protesters were Violent"
variables[5, 1] = "Reactive Repression Toleration"

variables<-variables %>% 
  dplyr::rename(
    outcome = matrix.NA..nrow...5..ncol...1.)

```

```{r}
tab_gm_2 <- cbind(tab_gm_2, outcome = variables$outcome)
```

```{r}
library(xtable)
xtable(tab_gm_2)
```

#Table A.6.2: t-test results comparing public reactions to male and patriarchy-compliant female protesters
```{r}
v1_t_tm <- t.test(violence_1 ~ condition_f,data = gp_tm)
m1_t_tm <- t.test(mor_1 ~ condition_f,data = gp_tm)
v2_t_tm <- t.test(violence_2 ~ condition_f,data = gp_tm)
t1_t_tm <- t.test(toler_1_index ~ condition_f,data = gp_tm)
t2_t_tm <- t.test(toler_2_index ~ condition_f,data = gp_tm)

```

```{r}
library(broom)
library(purrr)

tab_tm <- map_df(list(v1_t_tm, m1_t_tm, v2_t_tm, t1_t_tm, t2_t_tm), tidy)
```

```{r}
tab_tm_2 <- tab_tm[c("estimate1", "estimate2","estimate","statistic", "p.value", "parameter")]
```



```{r}
tab_tm_2 <- cbind(tab_tm_2, outcome = variables$outcome)

library(dplyr)
tab_tm_3 <- tab_tm_2 %>% dplyr::select(outcome, everything())
```

```{r}
library(xtable)
xtable(tab_tm_3)
```


#Table A.6.3: t-test results comparing public reactions to male and patriarchy-defiant female protesters
```{r}
v1_t_ntm <- t.test(violence_1 ~ condition_f,data = gp_ntm)
m1_t_ntm <- t.test(mor_1 ~ condition_f,data = gp_ntm)
v2_t_ntm <- t.test(violence_2 ~ condition_f,data = gp_ntm)
t1_t_ntm <- t.test(toler_1_index ~ condition_f,data = gp_ntm)
t2_t_ntm <- t.test(toler_2_index ~ condition_f,data = gp_ntm)

```

```{r}
library(broom)
library(purrr)

tab_ntm <- map_df(list(v1_t_ntm, m1_t_ntm, v2_t_ntm, t1_t_ntm, t2_t_ntm), tidy)
```

```{r}
tab_ntm_2 <- tab_ntm[c("estimate1", "estimate2","estimate","statistic", "p.value", "parameter")]
```

```{r}
tab_ntm_2 <- cbind(tab_ntm_2, outcome = variables$outcome)

library(dplyr)
tab_ntm_3 <- tab_ntm_2 %>% dplyr::select(outcome, everything())


```

```{r}
library(xtable)
xtable(tab_ntm_3)
```

#Table A.6.4: t-test results comparing reactions to patriarchy-compliant and patriarchy-defiant female protesters
```{r}
v1_t_tn <- t.test(violence_1 ~ condition_f,data = gp_tn)
m1_t_tn <- t.test(mor_1 ~ condition_f,data = gp_tn)
v2_t_tn <- t.test(violence_2 ~ condition_f,data = gp_tn)
t1_t_tn <- t.test(toler_1_index ~ condition_f,data = gp_tn)
t2_t_tn <- t.test(toler_2_index ~ condition_f,data = gp_tn)

```

```{r}
library(broom)
library(purrr)

tab_tn <- map_df(list(v1_t_tn, m1_t_tn, v2_t_tn, t1_t_tn, t2_t_tn), tidy)
```

```{r}
tab_tn_2 <- tab_tn[c("estimate1", "estimate2","estimate","statistic", "p.value", "parameter")]
```

```{r}
tab_tn_2 <- cbind(tab_tn_2, outcome = variables$outcome)

library(dplyr)
tab_tn_3 <- tab_tn_2 %>% dplyr::select(outcome, everything())
```

```{r}
library(xtable)
xtable(tab_tn_3)
```


##### ------------------  APPENDIX 7: MEDIATION  ANALYSIS RESULTS ----------------- ##### 


# Violence 1 as a Mediator


```{r}
extract_mediation_summary <- function (x) { 

  clp <- 100 * x$conf.level
  isLinear.y <- ((class(x$model.y)[1] %in% c("lm", "rq")) || 
                   (inherits(x$model.y, "glm") && x$model.y$family$family == 
                      "gaussian" && x$model.y$family$link == "identity") || 
                   (inherits(x$model.y, "survreg") && x$model.y$dist == 
                      "gaussian"))

  printone <- !x$INT && isLinear.y

  if (printone) {

    smat <- c(x$d1, x$d1.ci, x$d1.p)
    smat <- rbind(smat, c(x$z0, x$z0.ci, x$z0.p))
    smat <- rbind(smat, c(x$tau.coef, x$tau.ci, x$tau.p))
    smat <- rbind(smat, c(x$n0, x$n0.ci, x$n0.p))

    rownames(smat) <- c("ACME", "ADE", "Total Effect", "Prop. Mediated")

  } else {
    smat <- c(x$d0, x$d0.ci, x$d0.p)
    smat <- rbind(smat, c(x$d1, x$d1.ci, x$d1.p))
    smat <- rbind(smat, c(x$z0, x$z0.ci, x$z0.p))
    smat <- rbind(smat, c(x$z1, x$z1.ci, x$z1.p))
    smat <- rbind(smat, c(x$tau.coef, x$tau.ci, x$tau.p))
    smat <- rbind(smat, c(x$n0, x$n0.ci, x$n0.p))
    smat <- rbind(smat, c(x$n1, x$n1.ci, x$n1.p))
    smat <- rbind(smat, c(x$d.avg, x$d.avg.ci, x$d.avg.p))
    smat <- rbind(smat, c(x$z.avg, x$z.avg.ci, x$z.avg.p))
    smat <- rbind(smat, c(x$n.avg, x$n.avg.ci, x$n.avg.p))

    rownames(smat) <- c("ACME (control)", "ACME (treated)", 
                        "ADE (control)", "ADE (treated)", "Total Effect", 
                        "Prop. Mediated (control)", "Prop. Mediated (treated)", 
                        "ACME (average)", "ADE (average)", "Prop. Mediated (average)")

  }

  colnames(smat) <- c("Estimate", paste(clp, "% CI Lower", sep = ""), 
                      paste(clp, "% CI Upper", sep = ""), "p-value")
  smat

}
```

# Table A.7.1
```{r}
library("mediation")

set.seed(2014)
med.fit_v1_tn <- lm(violence_1 ~ cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_tn)
out.fit_v1_tn <- lm(toler_1_index ~ violence_1 + cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_tn)

med.out_v1_tn <- mediate(med.fit_v1_tn, out.fit_v1_tn, treat = "cond_3", mediator = "violence_1", boot = TRUE, sims = 1000)

summary(med.out_v1_tn)
plot(med.out_v1_tn)

```

```{r}
library(xtable)

df_med.out_v1_tn <- extract_mediation_summary(summary(med.out_v1_tn))

xtable(df_med.out_v1_tn)
```



# Table A.7.2
```{r}
library("mediation")

set.seed(2014)
med.fit_v1_tm <- lm(violence_1 ~ cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_tm)
out.fit_v1_tm <- lm(toler_1_index ~ violence_1 + cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_tm)

med.out_v1_tm <- mediate(med.fit_v1_tm, out.fit_v1_tm, treat = "cond_3", mediator = "violence_1", boot = TRUE, sims = 1000)
summary(med.out_v1_tm)
plot(med.out_v1_tm)

```

```{r}
df_med.out_v1_tm <- extract_mediation_summary(summary(med.out_v1_tm))

xtable(df_med.out_v1_tm)
```
# Table A.7.3

```{r}
library("mediation")

set.seed(2014)
med.fit_v1_gm <- lm(violence_1 ~ cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_gm)
out.fit_v1_gm <- lm(toler_1_index ~ violence_1 + cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_gm)

med.out_v1_gm <- mediate(med.fit_v1_gm, out.fit_v1_gm, treat = "cond_3", mediator = "violence_1", boot = TRUE, sims = 1000)

summary(med.out_v1_gm)
plot(med.out_v1_gm)

```

```{r}
df_med.out_v1_gm <- extract_mediation_summary(summary(med.out_v1_gm))

xtable(df_med.out_v1_gm)
```

# Table A.7.4

```{r}
library("mediation")

set.seed(2014)
med.fit_v1_ntm <- lm(violence_1 ~ cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_ntm)
out.fit_v1_ntm <- lm(toler_1_index ~ violence_1 + cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_ntm)

med.out_v1_ntm <- mediate(med.fit_v1_ntm, out.fit_v1_ntm, treat = "cond_3", mediator = "violence_1", boot = TRUE, sims = 1000)

summary(med.out_v1_ntm)
plot(med.out_v1_ntm)

```

```{r}
df_med.out_v1_ntm <- extract_mediation_summary(summary(med.out_v1_ntm))

xtable(df_med.out_v1_ntm)
```


# Violence 2 as a Mediator

# Table A.7.5

```{r}
library("mediation")

set.seed(2014)
med.fit_v2_tn <- lm(violence_2 ~ cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox +uni, data = gp_tn)
out.fit_v2_tn <- lm(toler_2_index ~ violence_2 + cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox +uni, data = gp_tn)

med.out_v2_tn <- mediate(med.fit_v2_tn, out.fit_v2_tn, treat = "cond_3", mediator = "violence_2", boot = TRUE, sims = 1000)

summary(med.out_v2_tn)
plot(med.out_v2_tn)

```

```{r}
df_med.out_v2_tn <- extract_mediation_summary(summary(med.out_v2_tn))

xtable(df_med.out_v2_tn)
```



# Table A.7.6

```{r}
library("mediation")

set.seed(2014)
med.fit_v2_tm <- lm(violence_2 ~ cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox +uni, data = gp_tm)
out.fit_v2_tm <- lm(toler_2_index ~ violence_2 + cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox +uni, data = gp_tm)

med.out_v2_tm <- mediate(med.fit_v2_tm, out.fit_v2_tm, treat = "cond_3", mediator = "violence_2", boot = TRUE, sims = 1000)

summary(med.out_v2_tm)
plot(med.out_v2_tm)

```



```{r}
df_med.out_v2_tm <- extract_mediation_summary(summary(med.out_v2_tm))

xtable(df_med.out_v2_tm)
```



# Perceived Immorality as a Mediator (Tolerance 1)

# Table A.7.7

```{r traditional vs. non-traditional women}
library("mediation")

set.seed(2014)
med.fit_m_t1_tn <- lm(mor_1 ~ cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_tn)
out.fit_m_t1_tn <- lm(toler_1_index ~ mor_1 + cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_tn)

med.out_m_t1_tn <- mediate(med.fit_m_t1_tn, out.fit_m_t1_tn, treat = "cond_3", mediator = "mor_1", boot = TRUE, sims = 1000)

summary(med.out_m_t1_tn)

```

```{r}
df_med.out_m_t1_tn <- extract_mediation_summary(summary(med.out_m_t1_tn))

xtable(df_med.out_m_t1_tn)
```



# Table A.7.8

```{r}
library("mediation")

set.seed(2014)
med.fit_m_t1_tm <- lm(mor_1 ~ cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_tm)
out.fit_m_t1_tm <- lm(toler_1_index ~ mor_1 + cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_tm)

med.out_m_t1_tm <- mediate(med.fit_m_t1_tm, out.fit_m_t1_tm, treat = "cond_3", mediator = "mor_1", boot = TRUE, sims = 1000)

summary(med.out_m_t1_tm)

```


```{r}
df_med.out_m_t1_tm <- extract_mediation_summary(summary(med.out_m_t1_tm))

xtable(df_med.out_m_t1_tm)
```



# Table A.7.9

```{r}
library("mediation")

set.seed(2014)
med.fit_m_t1_gm <- lm(mor_1 ~ cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_gm)
out.fit_m_t1_gm <- lm(toler_1_index ~ mor_1 + cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_gm)

med.out_m_t1_gm <- mediate(med.fit_m_t1_gm, out.fit_m_t1_gm, treat = "cond_3", mediator = "mor_1", boot = TRUE, sims = 1000)

summary(med.out_m_t1_gm)

```

```{r}
df_med.out_m_t1_gm <- extract_mediation_summary(summary(med.out_m_t1_gm))

xtable(df_med.out_m_t1_gm)
```


# Table A.7.10
```{r}
library("mediation")

set.seed(2014)
med.fit_m_t1_ntm <- lm(mor_1 ~ cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_ntm)
out.fit_m_t1_ntm <- lm(toler_1_index ~ mor_1 + cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_ntm)

med.out_m_t1_ntm <- mediate(med.fit_m_t1_ntm, out.fit_m_t1_ntm, treat = "cond_3", mediator = "mor_1", boot = TRUE, sims = 1000)

summary(med.out_m_t1_ntm)

```

```{r}
df_med.out_m_t1_ntm <- extract_mediation_summary(summary(med.out_m_t1_ntm))

xtable(df_med.out_m_t1_ntm)
```


#Immorality Perception (Reactive Repression Tolerance)

# Table A.7.11

```{r}
library("mediation")

set.seed(2014)
med.fit_m_t2_tn <- lm(mor_1 ~ cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_tn)
out.fit_m_t2_tn <- lm(toler_2_index ~ mor_1 + cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_tn)

med.out_m_t2_tn <- mediate(med.fit_m_t2_tn, out.fit_m_t2_tn, treat = "cond_3", mediator = "mor_1", boot = TRUE, sims = 1000)

summary(med.out_m_t2_tn)

```

```{r}
df_med.out_m_t2_tn <- extract_mediation_summary(summary(med.out_m_t2_tn))

xtable(df_med.out_m_t2_tn)
```


# Table A.7.12
```{r}
library("mediation")

set.seed(2014)
med.fit_m_t2_tm <- lm(mor_1 ~ cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_tm)
out.fit_m_t2_tm <- lm(toler_2_index ~ mor_1 + cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_tm)

med.out_m_t2_tm <- mediate(med.fit_m_t2_tm, out.fit_m_t2_tm, treat = "cond_3", mediator = "mor_1", boot = TRUE, sims = 1000)

summary(med.out_m_t2_tm)

```


```{r}
df_med.out_m_t2_tm <- extract_mediation_summary(summary(med.out_m_t2_tm))

xtable(df_med.out_m_t2_tm)
```

# Table A.7.13
```{r}
library("mediation")

set.seed(2014)
med.fit_m_t2_ntm <- lm(mor_1 ~ cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_ntm)
out.fit_m_t2_ntm <- lm(toler_2_index ~ mor_1 + cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_ntm)

med.out_m_t2_ntm <- mediate(med.fit_m_t2_ntm, out.fit_m_t2_ntm, treat = "cond_3", mediator = "mor_1", boot = TRUE, sims = 1000)

summary(med.out_m_t2_ntm)

```

```{r}
df_med.out_m_t2_ntm <- extract_mediation_summary(summary(med.out_m_t2_ntm))

xtable(df_med.out_m_t2_ntm)
```


#Immorality Perception (Reactive Violence Perception)

# Table A.7.14
```{r}
library("mediation")

set.seed(2014)
med.fit_m_v_tn <- lm(mor_1 ~ cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_tn)
out.fit_m_v_tn <- lm(violence_2 ~ mor_1 + cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_tn)

med.out_m_v_tn <- mediate(med.fit_m_v_tn, out.fit_m_v_tn, treat = "cond_3", mediator = "mor_1", boot = TRUE, sims = 1000)

summary(med.out_m_v_tn)

```


```{r}
df_med.out_m_v_tn <- extract_mediation_summary(summary(med.out_m_v_tn))

xtable(df_med.out_m_v_tn)
```


# Table A.7.15
```{r}
library("mediation")

set.seed(2014)
med.fit_m_v_tm <- lm(mor_1 ~ cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_tm)
out.fit_m_v_tm <- lm(violence_2 ~ mor_1 + cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_tm)

med.out_m_v_tm <- mediate(med.fit_m_v_tm, out.fit_m_v_tm, treat = "cond_3", mediator = "mor_1", boot = TRUE, sims = 1000)

summary(med.out_m_v_tm)

```

```{r}
df_med.out_m_v_tm <- extract_mediation_summary(summary(med.out_m_v_tm))

xtable(df_med.out_m_v_tm)
```



# Table A.7.16
```{r non-traditional women vs. men}
library("mediation")

set.seed(2014)
med.fit_m_v_ntm <- lm(mor_1 ~ cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_ntm)
out.fit_m_v_ntm <- lm(violence_2 ~ mor_1 + cond_3 + age_b_st + sex_b + income_b_st + putin_support_st + orthodox + uni, data = gp_ntm)

med.out_m_v_ntm <- mediate(med.fit_m_v_ntm, out.fit_m_v_ntm, treat = "cond_3", mediator = "mor_1", boot = TRUE, sims = 1000)

summary(med.out_m_v_ntm)

```


```{r}
df_med.out_m_v_ntm <- extract_mediation_summary(summary(med.out_m_v_ntm))

xtable(df_med.out_m_v_ntm)
```




#Table A.7.17: The mediating effect of reactive violence perception (main mediator) and perceived immorality (alternative mediator) on support for repressing patriarchy-compliant vs. patriarchy-defiant female protesters
```{r}
Xnames <- c("age_b_st", "sex_b", "income_b_st", "putin_support_st","orthodox","uni")

m.med_tn_v2_m <- multimed(outcome = "toler_2_index", med.main = "violence_2",med.alt = "mor_1", treat = "cond_3", covariates = Xnames, data = gp_tn, sims = 1000)
summary(m.med_tn_v2_m)

#Sample Size Calculation
complete_rows_count <- sum(complete.cases(gp_tn[c("age_b_st", "sex_b", "income_b_st", "putin_support_st","orthodox","uni","toler_2_index", "violence_2", "mor_1", "cond_3")]))
print(complete_rows_count)
```


#Figure A.7.1
```{r}
plot(m.med_tn_v2_m,type="point")

```

#Table A.7.18: The mediating effect of reactive violence perception (main mediator) and perceived immorality (alternative mediator) on support for repressing patriarchy-compliant female vs. male protesters
```{r}
Xnames <- c("age_b_st", "sex_b", "income_b_st", "putin_support_st","orthodox","uni")

m.med_tm_v2_m <- multimed(outcome = "toler_2_index", med.main = "violence_2",med.alt = "mor_1", treat = "cond_3", covariates = Xnames, data = gp_tm, sims = 1000)
summary(m.med_tm_v2_m)

#Sample Size Calculation
complete_rows_count_tm <- sum(complete.cases(gp_tm[c("age_b_st", "sex_b", "income_b_st", "putin_support_st","orthodox","uni","toler_2_index", "violence_2", "mor_1", "cond_3")]))
print(complete_rows_count_tm)

```

#Figure A.7.2
```{r}
plot(m.med_tm_v2_m,type="point")

```

##### ------------------  APPENDIX 8: MEDIATION SENSITIVITY PLOTS ----------------- ##### 


#Figure A.8.1.1

```{r}

summary(med.out_v2_tn)

sens.out_v2_tn <- medsens(med.out_v2_tn, rho.by = 0.1, effect.type = "indirect", sims = 1000)
summary(sens.out_v2_tn)


```

```{r}
plot(sens.out_v2_tn, sens.par = "rho", main = "Traditional vs. Non-Traditional Female Protesters", ylim = c(-0.7, 0.7))
plot(sens.out_v2_tn, sens.par = "R2", r.type = "total", sign.prod = "positive")

```



#Figure A.8.1.2

```{r}
summary(med.out_v2_tm)

sens.out_v2_tm <- medsens(med.out_v2_tm, rho.by = 0.1, effect.type = "indirect", sims = 1000)
summary(sens.out_v2_tm)


```

```{r}
plot(sens.out_v2_tm, sens.par = "rho", main = "Reactive Violence Perception", ylim = c(-0.7, 0.7))

plot(sens.out_v2_tm, sens.par = "R2", r.type = "total", sign.prod = "positive")
```


#Imorality

#Figure A.8.2.1

```{r}
summary(med.out_m_t1_tn)

sens.out_m_t1_tn <- medsens(med.out_m_t1_tn, rho.by = 0.1, effect.type = "indirect", sims = 1000)
summary(sens.out_m_t1_tn)

```

```{r}
plot(sens.out_m_t1_tn, sens.par = "rho", main = " ", ylim = c(-0.7, 0.7))

plot(sens.out_m_t1_tn, sens.par = "R2", r.type = "total", sign.prod = "positive")
```

#Figure A.8.2.2

```{r}
summary(med.out_m_t1_tm)

sens.out_m_t1_tm <- medsens(med.out_m_t1_tm, rho.by = 0.1, effect.type = "indirect", sims = 1000)
summary(sens.out_m_t1_tm)

```

```{r}
plot(sens.out_m_t1_tm, sens.par = "rho", main = " ", ylim = c(-0.7, 0.7))

plot(sens.out_m_t1_tm, sens.par = "R2", r.type = "total", sign.prod = "positive")
```


#Figure A.8.2.3

```{r}
summary(med.out_m_t1_ntm)

sens.out_m_t1_ntm <- medsens(med.out_m_t1_ntm, rho.by = 0.1, effect.type = "indirect", sims = 1000)
summary(sens.out_m_t1_ntm)

```

```{r}
plot(sens.out_m_t1_ntm, sens.par = "rho", main = " ", ylim = c(-0.7, 0.7))

plot(sens.out_m_t1_ntm, sens.par = "R2", r.type = "total", sign.prod = "positive")
```

#Figure A.8.3.1

```{r}
summary(med.out_m_t2_tn)

sens.out_m_t2_tn <- medsens(med.out_m_t2_tn, rho.by = 0.1, effect.type = "indirect", sims = 1000)
summary(sens.out_m_t2_tn)

```

```{r}
plot(sens.out_m_t2_tn, sens.par = "rho", main = " ", ylim = c(-0.7, 0.7))

plot(sens.out_m_t2_tn, sens.par = "R2", r.type = "total", sign.prod = "positive")
```

#Figure A.8.3.2

```{r}
summary(med.out_m_t2_tm)

sens.out_m_t2_tm <- medsens(med.out_m_t2_tm, rho.by = 0.1, effect.type = "indirect", sims = 1000)
summary(sens.out_m_t2_tm)

```

```{r}
plot(sens.out_m_t2_tm, sens.par = "rho", main = " ", ylim = c(-0.7, 0.7))

plot(sens.out_m_t2_tm, sens.par = "R2", r.type = "total", sign.prod = "positive")
```

#Figure A.8.3.3

```{r}
summary(med.out_m_t2_ntm)

sens.out_m_t2_ntm <- medsens(med.out_m_t2_ntm, rho.by = 0.1, effect.type = "indirect", sims = 1000)
summary(sens.out_m_t2_ntm)

```

```{r}
plot(sens.out_m_t2_ntm, sens.par = "rho", main = " ", ylim = c(-0.7, 0.7))

plot(sens.out_m_t2_ntm, sens.par = "R2", r.type = "total", sign.prod = "positive")
```


#Figure A.8.4.1

```{r}
summary(med.out_m_v_tn)

sens.out_m_v_tn <- medsens(med.out_m_v_tn, rho.by = 0.1, effect.type = "indirect", sims = 1000)
summary(sens.out_m_t1_tn)

```

```{r}
plot(sens.out_m_v_tn, sens.par = "rho", main = " ", ylim = c(-0.7, 0.7))

plot(sens.out_m_v_tn, sens.par = "R2", r.type = "total", sign.prod = "positive")
```


#Figure A.8.4.2
```{r}
summary(med.out_m_v_tm)

sens.out_m_v_tm <- medsens(med.out_m_v_tm, rho.by = 0.1, effect.type = "indirect", sims = 1000)
summary(sens.out_m_v_tm)

```

```{r}
plot(sens.out_m_v_tm, sens.par = "rho", main = " ", ylim = c(-0.7, 0.7))

plot(sens.out_m_v_tm, sens.par = "R2", r.type = "total", sign.prod = "positive")
```


#Figure A.8.4.3

```{r}
summary(med.out_m_v_ntm)


sens.out_m_v_ntm <- medsens(med.out_m_v_ntm, rho.by = 0.1, effect.type = "indirect", sims = 1000)
summary(sens.out_m_v_ntm)

```

```{r}
plot(sens.out_m_v_ntm, sens.par = "rho", main = " ", ylim = c(-0.7, 0.7))

plot(sens.out_m_v_ntm, sens.par = "R2", r.type = "total", sign.prod = "positive")
```

##### ------------------  APPENDIX 9: PATRIARCHAL VALUES IN RUSSIA ----------------- #####
#World Values Surveys (WVS) Datasets can be downloaded at the following link: https://www.worldvaluessurvey.org/WVSDocumentationWVL.jsp

#Figure A.9.1: Patriarchal Scores by Country (WVS-6, 2010-2014)
```{r}
setwd("/Users/martinnaunov/Desktop/Gender Protesting Data")
Wave6 <- read.csv2("WV6_Data_csv_v20201117.csv",na="NA", dec=";")

```

```{r}
###################### wave 6  ########################

# V2: country
# V50: Pre-school child suffers with working mother 
# V51: (Q29) Men make better political leaders than women so
# V52: (Q30) University is more important for a boy than for a girl
# V53 (Q31) Men make better business executives than women do
# V54: (Q32) Being a housewife just as fulfilling
# V45: (Q33) Jobs scarce: Men should have more right to a job than women
# V47: Q35: Problem is women have more income than husband
# V206: (Q186) Justifiable - Sex before Marriage 
# V208 (Q189) Justifiable - For a man to beat his wife
# V205: Justifiable: Divorce

wanted_cols_6 = c("V2","V50", "V51", "V52","V53", "V54", "V45","V47", "V206", "V208", "V205")
df_6 = Wave6[,wanted_cols_6]

```


```{r}
df_6$V50[df_6$V50 < 0] = NA          
df_6$V51[df_6$V51 < 0] = NA           
df_6$V52[df_6$V52 < 0] = NA           
df_6$V53[df_6$V53 < 0] = NA           
df_6$V54[df_6$V54 < 0] = NA           
df_6$V45[df_6$V45 < 0] = NA           
df_6$V47[df_6$V47 < 0] = NA           
df_6$V205[df_6$V205 < 0] = NA         
df_6$V206[df_6$V206 < 0] = NA         
df_6$V208[df_6$V208 < 0] = NA         

#Reverse coding
df_6$V208 <- abs(df_6$V208-11)

```

```{r}
df_6$V50 <- range01(df_6$V50)
df_6$V51 <- range01(df_6$V51)
df_6$V52 <- range01(df_6$V52)
df_6$V53 <- range01(df_6$V53)
df_6$V54 <- range01(df_6$V54)
df_6$V45 <- range01(df_6$V45)
df_6$V47 <- range01(df_6$V47)
df_6$V205 <- range01(df_6$V205)
df_6$V206 <- range01(df_6$V206)
df_6$V208 <- range01(df_6$V208)


```


```{r}

df_6$patriarchy_index <- apply(df_6[,c("V50","V51","V52","V53","V54","V45","V47","V205","V206","V208")],1,mean,na.rm=TRUE)

av.selecteditems = mean(df_6$patriarchy_index, na.rm = TRUE)
# World Average: 0.528459

df_6$Country = countrycode(sourcevar = df_6$V2, origin = 'iso3n', destination = 'un.name.en', warn = FALSE)

df_6$Country <- ifelse(df_6$Country == "United States of America", "USA", df_6$Country)
df_6$Country <- ifelse(df_6$Country == "Russian Federation", "Russia", df_6$Country)
df_6$Country <- ifelse(df_6$Country == "Trinidad and Tobago", "Trinidad", df_6$Country)
df_6$Country <- ifelse(df_6$Country == "Republic of Korea", "S. Korea", df_6$Country)


df_6$Country = as.factor(df_6$Country)

summary(df_6$patriarchy_index[df_6$Country=="Russia"])

#Russia Mean: 0.544

```


```{r}

list_6 = aggregate(patriarchy_index ~ Country, data = df_6, FUN = mean)
#Russia: 0.5444113

list_6 = aggregate(patriarchy_index ~ Country, data = df_6, FUN = mean)

list_6 = arrange(list_6, by = patriarchy_index)

list_6$is_russia = "Other"

list_6$is_russia[list_6$Country=="Russia"] = "Russia"

list_6 = list_6[is.na(list_6$Country) == FALSE,]

av_round_6 = "Worldwide Average = 0.53"


```


```{r}
library(ggplot2)
library(wesanderson)

g_6 = ggplot(list_6, aes(x = reorder(Country, -patriarchy_index), y = patriarchy_index, fill = is_russia, color = is_russia)) + 
  geom_col(alpha = .5, width = .75) + 
  ylab("Patriarchal Values Score") + 
  xlab("Country") + 
  theme_minimal() +
  theme(axis.text.x=element_text(angle = 90, hjust = 1, vjust = 0.5)) +
  theme(legend.position = "none") +
  geom_hline(yintercept = av.selecteditems) + 
  annotate("text", x = 40, y = 0.5, label = av_round_6) +
  scale_fill_manual(values = rev(wes_palette("Darjeeling1"))) + 
  scale_color_manual(values = rev(wes_palette("Darjeeling1"))) + 
  ggtitle("Patriarchal Values Score by Country (WVS-6)")
g_6
```


#Figure A.9.2: Patriarchal Scores by Country (WVS-7, 2017-2020)
```{r}
setwd("/Users/martinnaunov/Desktop/Gender Protesting Data")
Wave7 <- read.csv2("WVS_Cross-National_Wave_7_csv_v3_0.csv",na="NA", dec=";")
```

```{r}
# B_COUNTRY: country
# Q28: Pre-school child suffers with working mother 
# (Q29) Men make better political leaders than women so
# (Q30) University is more important for a boy than for a girl
# (Q31) Men make better business executives than women do
# (Q32) Being a housewife just as fulfilling
# (Q33) Jobs scarce: Men should have more right to a job than women
# Q35: Problem is women have more income than husband
# (Q186) Justifiable - Sex before Marriage 
# V208 (Q189) Justifiable - For a man to beat his wife
# Q185: Justifiable: Divorce


wanted_cols_7 = c("B_COUNTRY", "Q28", "Q29", "Q30", "Q31", "Q32","Q33", "Q35", "Q186","Q189","Q185")
df_7 = Wave7[,wanted_cols_7]


df_7$Q28[df_7$Q28 < 0] = NA
df_7$Q29[df_7$Q29 < 0] = NA
df_7$Q30[df_7$Q30 < 0] = NA
df_7$Q31[df_7$Q31 < 0] = NA
df_7$Q32[df_7$Q32 < 0] = NA
df_7$Q33[df_7$Q33 < 0] = NA
df_7$Q35[df_7$Q35 < 0] = NA
df_7$Q186[df_7$Q186 < 0] = NA
df_7$Q189[df_7$Q189 < 0] = NA
df_7$Q185[df_7$Q185 < 0] = NA

#Reverse coding
df_7$Q189 <- abs(df_7$Q189-11)


```



```{r Standardize}

range01 <- function(x,na.rm = T){(x-min(x, na.rm = T))/(max(x,na.rm = T)-min(x,na.rm = T))}
df_7$Q28 <- range01(df_7$Q28)
df_7$Q29 <- range01(df_7$Q29)
df_7$Q30 <- range01(df_7$Q30)
df_7$Q31 <- range01(df_7$Q31)
df_7$Q32 <- range01(df_7$Q32)
df_7$Q33 <- range01(df_7$Q33)
df_7$Q35 <- range01(df_7$Q35)
df_7$Q186 <- range01(df_7$Q186)
df_7$Q189 <- range01(df_7$Q189)
df_7$Q185 <- range01(df_7$Q185)

```



```{r}

df_7$patriarchy_index <- apply(df_7[,c("Q28","Q29","Q30","Q31","Q32","Q33","Q35", "Q186","Q189","Q185")],1,mean,na.rm=TRUE)
summary(df_7$patriarchy_index)
#Mean=0.5434
av_7 = mean(df_7$patriarchy_index, na.rm = TRUE)

#0.5433961
```

```{r}

library(countrycode)
df_7$Country = countrycode(sourcevar = df_7$B_COUNTRY, origin = 'iso3n', destination = 'un.name.en', warn = FALSE)

# some values didn't match-- probably because they're all territories 

df_7$Country[df_7$B_COUNTRY==158] = "Taiwan"

df_7$Country[df_7$B_COUNTRY==344] = "Hong Kong"

df_7$Country[df_7$B_COUNTRY==630] = "Puerto Rico"

df_7$Country[df_7$B_COUNTRY==862] = "Venezuela"

df_7$Country[df_7$B_COUNTRY==840] = "USA"

df_7$Country[df_7$B_COUNTRY==364] = "Iran"

df_7$Country[df_7$B_COUNTRY==68] = "Bolivia"

df_7$Country[df_7$B_COUNTRY==410] = "South Korea"

df_7$Country[df_7$B_COUNTRY==643] = "Russia"

df_7$Country = as.factor(df_7$Country)

summary(df_7$patriarchy_index[df_7$Country=="Russia"])

#Russia Mean: 0.5472
```

```{r}

list_7 = aggregate(patriarchy_index ~ Country, data = df_7, FUN = mean)

list_7 = arrange(list_7, by = patriarchy_index)

list_7$is_russia = "Other"

list_7$is_russia[list_7$Country=="Russia"] = "Russia"

list_7 = list_7[is.na(list_7$Country) == FALSE,]

av_round_7 = "Worldwide Average = 0.54"

```

```{r}
library(ggplot2)
library(wesanderson)

g = ggplot(list_7, aes(x = reorder(Country, -patriarchy_index), y = patriarchy_index, fill = is_russia, color = is_russia)) + 
  geom_col(alpha = .5, width = .75) + 
  ylab("Patriarchal Values Score") + 
  xlab("Country") + 
  theme_minimal() +
  theme(axis.text.x=element_text(angle = 90, hjust = 1, vjust = 0.5)) +
  theme(legend.position = "none") +
  geom_hline(yintercept = av_7) + 
  annotate("text", x = 40, y = 0.5, label = av_round_7) +
  scale_fill_manual(values = rev(wes_palette("Darjeeling1"))) + 
  scale_color_manual(values = rev(wes_palette("Darjeeling1"))) + 
  ggtitle("Patriarchal Values Score by Country (WVS-7)")
g
```


